package com.ajl.wx.module.course.controller;

import com.ajl.wx.beans.po.CoursePO;
import com.ajl.wx.beans.vo.ResultApi;
import com.ajl.wx.config.WebApplicationProperties;
import com.ajl.wx.module.course.service.impl.TbCourseServiceImpl;
import com.ajl.wx.utils.ResultApiUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 小程序接口
 */
@RestController
@RequestMapping("/api/course/")
public class CourseController {

    @Autowired
    WebApplicationProperties webApplicationProperties;

    @Autowired
    TbCourseServiceImpl courseService;


    /**
     * 分页获取课程,推荐观看量前十的数据
     *
     * @param page
     * @return
     */
    @GetMapping("/listCourse")
    public ResultApi listCourse(@RequestParam(value = "type", required = false, defaultValue = "N/A") String courseType,
                                @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
        if ("N/A".equals(courseType)) {
            return ResultApiUtil.success("OK", courseService.listHot());
        }
        PageHelper.startPage(page, webApplicationProperties.getPagehelperPageSize());
        return ResultApiUtil.success("OK", courseService.listCourseByType(courseType));
    }

    @GetMapping("listTypes")
    public ResultApi listTypes() {
        return ResultApiUtil.success("OK", courseService.listTypes());
    }


    /**
     * 增加阅读数
     * @param courseId
     * @return
     */
    @GetMapping("watchCourse")
    public ResultApi watchCourse(@RequestParam(value = "id", required = false, defaultValue = "-1") int courseId) {
        CoursePO course = courseService.getById(courseId);
        if (course != null) {
            course.setWatchNumber(course.getWatchNumber() + 1);
            courseService.updateById(course);
            return ResultApiUtil.success("OK", courseService.updateById(course));
        }

        return ResultApiUtil.fail(-1,"课程为空");
    }


}
